package engine.gui;

import static org.lwjgl.opengl.GL11.*;

import java.io.IOException;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import engine.entities.AbstractEntity;

public class ButtonTextured extends AbstractEntity
{
	private boolean isEnabled=true, isTop=false, isClicked=false, isButtonDown=false;
	private Texture texEnabled,texTop,texClicked;
	
	public ButtonTextured(double x, double y, double width, double height, String texEnabled, String texTop, String texClicked)
	{
		super(x, y, width, height);
		try
		{
			this.texEnabled=TextureLoader.getTexture("PNG", getClass().getResourceAsStream(texEnabled));
			this.texTop=TextureLoader.getTexture("PNG", getClass().getResourceAsStream(texTop));
			this.texClicked=TextureLoader.getTexture("PNG", getClass().getResourceAsStream(texClicked));
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public void render()
	{
		if(!isEnabled)
			return;
		if(!isTop)
			texEnabled.bind();
		else if(isButtonDown)
			texClicked.bind();
		else
			texTop.bind();
		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2d(x, y);
			glTexCoord2f(1, 0);
			glVertex2d(x+width, y);
			glTexCoord2f(1, 1);
			glVertex2d(x+width, y+height);
			glTexCoord2f(0, 1);
			glVertex2d(x, y+height);
		glEnd();
		
	}
	
	@Override
	public void update(int delta)
	{
		if(!isEnabled)
			return;
		if( x<Mouse.getX() && x+width>Mouse.getX() && y<Display.getHeight()-Mouse.getY() && y+height>Display.getHeight()-Mouse.getY() )
		{
			if(Mouse.isButtonDown(0))
			{
				isButtonDown=true;
			}
			else if(isButtonDown==true)
			{
				isButtonDown=false;
				isClicked=true;
			}
			isTop=true;
		}
		else isTop=false;
	}
	
	public boolean isClicked()
	{
		return isClicked;
	}
	public void clickProcessed()
	{
		isClicked=false;
	}
}
